package com.hluck.mvidemo.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import com.hluck.mvidemo.R
import com.hluck.mvidemo.data.intent.MainIntent
import com.hluck.mvidemo.data.state.MainState
import com.hluck.mvidemo.databinding.ActivityMainBinding
import com.hluck.mvidemo.network.NetworkUtils
import com.hluck.mvidemo.ui.adapter.WallpaperAdapter
import com.hluck.mvidemo.ui.viewmodel.MainViewModel
import com.hluck.mvidemo.ui.viewmodel.ViewModelFactory
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private lateinit var binding:ActivityMainBinding

    private lateinit var mainViewModel:MainViewModel

    private var wallPaperAdapter = WallpaperAdapter(arrayListOf())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        mainViewModel = ViewModelProvider(this,ViewModelFactory(NetworkUtils.apiService))[MainViewModel::class.java]

        initView()

        observeViewModel()
    }

    /**
     * 初始化
     */
    private fun initView(){
        binding.rvWallpaper.apply {
            layoutManager = GridLayoutManager(this@MainActivity,2)
            adapter = wallPaperAdapter
        }
        binding.btnGetWallpaper.setOnClickListener {
            lifecycleScope.launch {
                mainViewModel.mainIntentChannel.send(MainIntent.GetWallpaper)
            }
        }
    }

    /**
     * 观察viewModel
     */
    private fun observeViewModel(){
        lifecycleScope.launch {
            mainViewModel.state.collect{
                when(it){
                    is MainState.Idle ->{}
                    is MainState.Loading -> {
                        binding.btnGetWallpaper.visibility = View.GONE
                        binding.pbLoading.visibility = View.VISIBLE
                    }
                    is MainState.Wallpapers -> {
                        binding.btnGetWallpaper.visibility = View.GONE
                        binding.pbLoading.visibility = View.GONE
                        binding.rvWallpaper.visibility = View.VISIBLE
                        it.wallpaper.let { paper ->
                            wallPaperAdapter.addData(paper.res.vertical)
                        }
                        wallPaperAdapter.notifyDataSetChanged()
                    }
                    is MainState.Error -> {
                        binding.pbLoading.visibility = View.VISIBLE
                        binding.btnGetWallpaper.visibility = View.VISIBLE
                        Log.d("TAG", "observerViewModel:${it.error}")
                        Toast.makeText(this@MainActivity,it.error,Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }
    }
}